{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# default_exp core"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Core\n",
    "\n",
    "> Data preparation utility."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hide\n",
    "from nbdev.showdoc import *\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from sklearn.datasets import make_classification\n",
    "from sklearn.model_selection import train_test_split as tts\n",
    "from sklearn.datasets import fetch_openml\n",
    "\n",
    "SEED = 41\n",
    "np.random.seed(SEED)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "_all_ = ['get_example_dataset', 'get_mnist_dataset']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "def get_example_dataset(SEED):\n",
    "    X, y = make_classification(n_samples = 100, n_features=5, random_state=SEED)\n",
    "    Xtr, Xte, ytr, yte = tts(X, y, test_size=0.2, random_state=SEED)\n",
    "    \n",
    "    return Xtr, Xte, ytr, yte"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#export\n",
    "def get_mnist_dataset(SEED):\n",
    "    # Load data from https://www.openml.org/d/554\n",
    "    X, y = fetch_openml('mnist_784', version=1, return_X_y=True)\n",
    "    X = X / 255.\n",
    "    \n",
    "    ridx = np.random.permutation(len(X))\n",
    "    X_random = X[ridx]\n",
    "    y_random = y[ridx]\n",
    "    \n",
    "    idxs = np.where((y_random == '2') | (y_random == '6'))[0]\n",
    "    Xs, ys = X_random[idxs], y_random[idxs]\n",
    "    \n",
    "    Xtr, Xte, ytr, yte = tts(Xs, ys, test_size=0.2, random_state=SEED)\n",
    "    Xtr, Xva, ytr, yva = tts(Xtr, ytr, test_size=0.2, random_state=SEED)\n",
    "    \n",
    "    return (Xtr, Xva, Xte, ytr, yva, yte)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Export"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Converted 00_core.ipynb.\n",
      "Converted 01_Representation.ipynb.\n",
      "Converted 02_SymbolicInstance.ipynb.\n",
      "Converted index.ipynb.\n"
     ]
    }
   ],
   "source": [
    "from nbdev.export import *\n",
    "notebook2script()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
